﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UpdatePosting : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        FillForm();
    }

    void FillForm()
    {
        PostingDAO PostDAO = new PostingDAO();
        CompetitionDAO comDAO = new CompetitionDAO();
        Int64 postingID = Int64.Parse(Request["PostingID"].ToString());
        PostingInfo postInfo = PostDAO.GetPostingById(postingID);
        CompetitionDAO competDAO = new CompetitionDAO();
        IList<CompetitionInfo> listCompet = competDAO.GetAll();

        txtPostName.Text = postInfo.PostingName;
       
        txtDescription.Text = postInfo.Description;

        txtPrice.Text = Convert.ToString(postInfo.Price);

        txtQuotation.Text = postInfo.Quotations;
        drlCompetition.DataSource = listCompet;
        drlCompetition.DataTextField = "CompetitionName";
        drlCompetition.DataValueField = "CompetitionID";
        drlCompetition.DataBind();

        //post.CompetitionId = 2;
     
    }
    
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PostingDAO PostDAO = new PostingDAO();
        CompetitionDAO comDAO = new CompetitionDAO();
        Int64 postingID = Int64.Parse(Request["PostingID"].ToString());
        PostingInfo postLoad = PostDAO.GetPostingById(postingID);
        PostingInfo postUpdate = new PostingInfo();

        postUpdate.PostingId = postingID;
        postUpdate.PostingName = txtPostName.Text;
        postUpdate.PostDate = DateTime.Now;
        postUpdate.Description = txtDescription.Text;

            if (txtPrice.Text != "")
                postUpdate.Price = double.Parse(txtPrice.Text);

            postUpdate.Quotations = txtQuotation.Text;
            postUpdate.CompetitionId = Int32.Parse(drlCompetition.SelectedValue);


           //   try
             //   {
                    if (fuPainting.HasFile)
                    {

                        int intFileSizeLimit = 2000;
                        string postedFile = fuPainting.PostedFile.FileName;
                        string extension = System.IO.Path.GetExtension(postedFile);
                        string fileName = System.IO.Path.GetFileName(postedFile);
                        int size = fuPainting.PostedFile.ContentLength / 1024;
                        extension = extension.ToLower();

                        // if (fuPainting.PostedFile.ContentType == "image/jpeg")
                        if (extension.Equals(".jpg") || extension.Equals(".gif"))
                        {
                            if (size < intFileSizeLimit)
                            {
                                postUpdate.Url = fuPainting.FileName.ToString();
                                fuPainting.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
                                lblMessage.ForeColor = System.Drawing.Color.White;
                                lblMessage.Text = "<br /><br />" + "Uploaded file details <hr />" +
                                    "File Name: " + fileName + "<br />" +
                                    "File Extension Name: " + extension + "<br />" +
                                    "File Size: " + size.ToString() + "KB";
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "File size exceeded than limit!", true);
                            }
                        }
                        else
                        {

                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "Only image/jpeg file are allowed, try again!", true);

                        }



                    }
                    else { postUpdate.Url = postLoad.Url; }

        //        }
           //     catch (Exception ex)
         //       {
           //         string message = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
           ////         ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", message, true);
          //      }
         
        int update = PostDAO.StudentUpdatePosting(postUpdate);
            // if (fuPainting.PostedFile.ContentType == "image/jpeg")
         if (fuPainting.HasFile){
            if (System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".jpg") || System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".gif"))
            {
                

                if (update > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Upload Successfull!');", true);
                }
                else ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Upload Failed!');", true);
            } 
            else ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "Only image/jpeg file are allowed, try again!", true);
         }
         else if (update > 0)
         {
             ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Upload Successfull!');", true);
         }
         else
         {
             ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Upload Failed!');", true);
         }

  //      }
  //      catch (Exception ex)
   //     {
    //        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", ex.Message , true);
    //    }




    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

        txtDescription.Text = "";
        txtPostName.Text = "";
        txtPrice.Text = "";
        txtQuotation.Text = "";
        lblMessage.Text = "";
        Label1.Text = "";


    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin/MaintainPostings.aspx");
    }
}